package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Transactional
@Slf4j
@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    ApplicationMapper applicationMapper;
    @Autowired
    AuditService auditService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AuditMapper auditMapper;
    @Autowired
    VehicleMapper vehicleMapper;


    @Override
    public void save(ApplicationSaveParam applicationSaveParam) {
        log.debug("保存申请单,参数={}", applicationSaveParam);
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam, application);
        //申请单的初始状态为"已发起"
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setCreateTime(new Date());

        /* 到的问题:新增申请单时,无法给对应的审批单数据传入申请单id*/
        //原因:id是一个比较特殊的属性,无法直接通过某一个条件select获取
        /* 解决方案:给insert方法对应的SQL语句上加:
        useGeneratedKeys="true" keyProperty="id"*/
        //效果:JDBC会为当前application对象自动生成自增主键,并且把生成的主键值
        //赋值给当前application对象的id属性
        applicationMapper.insert(application);
        //还需要为此申请单生成对应的审批单数据
        auditService.insertAudit(application);
    }

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
        log.debug("查询申请单列表业务,参数={}", applicationQuery);
        List<ApplicationVO> list = applicationMapper.selectApplication(applicationQuery);
        //遍历每一个申请单VO,依次给每一个申请单VO补充审批人数据
        for(int i = 0; i < list.size(); i++){
            ApplicationVO  applicationVO = list.get(i);
            //处理当前的申请单VO,可以调用一个本类的自定义功能方法,添加审批人数据
            assignAuditUserList(applicationVO);
        }
        return list;
    }

    @Override
    public void cancel(Long id) {
        log.debug("撤销申请:id={}",id);
        Application application = new Application();
        application.setId(id);
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        auditMapper.deleteByApplicationId(id);
    }

    @Override
    public void distribute(Long applicationId, Long vehicleId) {
        log.debug("分配车辆:applicationId={},vehicleId={}",applicationId,vehicleId);
        Application application = new Application();
        application.setId(applicationId);
        application.setVehicleId(vehicleId);
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);

        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("2");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }

    @Override
    public void back(Long applicationId, Long vehicleId) {
        log.debug("还车:applicationId={},vehicleId={}",applicationId,vehicleId);
        Application application = new Application();
        application.setId(applicationId);
        application.setVehicleId(vehicleId);
        application.setStatus(ApplicationStatusEnum.END.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);

        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }

    /*本方法用于给当前传入的申请单VO的审批人数据赋值auditUserIdList与auditUsernameList*/
    private void assignAuditUserList(ApplicationVO applicationVO) {
        //1.准备空集合,分别用来存放审批人id与姓名
        List<String> auditUsernameList = new ArrayList<>();
        List<Long> auditUserIdList = new ArrayList<>();
        //2.根据传入的申请单vo对象,根据其申请单id可以查到对应的审批单对象
        // (注意:一个申请单可能对应多个审批单对象!)
        List<AuditVO> auditVOList =
                auditService.selectAuditByApplicationId(applicationVO.getId());
        //3.依次遍历每一个审批单对象
        for(int i = 0; i < auditVOList.size();i++){
            AuditVO auditVO = auditVOList.get(i);
            //4.获取当前遍历到的审批单中的审批人id值,并存入上方的空集合中
            Long id = auditVO.getAuditUserId();
            auditUserIdList.add(id);
            //5.根据审批人id到用户模块查出审批人姓名,并存入上方的空集合中
            UserVO userVO = userMapper.selectById(id);
            auditUsernameList.add(userVO.getUsername());
        }
        //6.需要准备一个拼接工具,帮我们将所有审批人姓名连成一个字符串
        StringJoiner stringJoiner = new StringJoiner(",");
        for(String username : auditUsernameList){
            stringJoiner.add(username);
        }
        //将准备好的审批人id集合赋值给申请单VO参数的审批人id集合属性
        applicationVO.setAuditUserIdList(auditUserIdList);
        //将准备好的审批人姓名集合赋值给申请单VO参数的审批人姓名字符串属性
        applicationVO.setAuditUsernameList(stringJoiner.toString());
    }
}
